<template>
    <div class="login-box">
      <van-nav-bar
      title="登录"
      left-text="返回"
       left-arrow
       @click-left="onClickLeft"/>
      <van-form @submit="onSubmit" ref="loginForm">
        <van-field
          v-model="user.mobile"
          name="mobile"
          placeholder="请输入手机号"
          type="number"
          maxlength="11"
          :rules="userFormRules.mobile">
          <i slot='left-icon' class="iconfont iconshouji"></i>
        </van-field>
        <van-field
          v-model="user.code"
          type="number"
          maxlength="6"
          name="code"
          placeholder="请输入验证码"
          :rules="userFormRules.code">
          <i slot='left-icon' class="iconfont iconyanzhengma"></i>
          <template #button>
            <van-count-down :time="time" format="ss 秒" v-if="isCountDownShow" ref="countDown" @finish='isCountDownShow = false' />
            <van-button size="mini" round native-type="button" class="send-code-btn" @click="onSendSms" v-else>发送验证码</van-button>
          </template>
        </van-field>
        <div class="login-btn-box">
          <van-button block type="info" native-type="submit">
            登录
          </van-button>
        </div>
      </van-form>
    </div>
</template>

<script>
import { login, getCode } from '@/api/user.js'
import { mapMutations } from 'vuex'
export default {
  name: 'login',
  data () {
    return {
      user: {
        mobile: '13911111111',
        code: '246810'
      },
      userFormRules: {
        mobile: [{ required: true, message: '请填写手机号' },
          { pattern: /^1\d{10}$/, message: '手机号格式不正确' }],
        code: [{ required: true, message: '请填写验证码' },
          { pattern: /^\d{6}$/, message: '验证码格式不正确' }]
      },
      time: 10 * 1000,
      isCountDownShow: false
    }
  },

  created () {

  },

  methods: {
    ...mapMutations(['setUser']),
    // 提交表单
    async onSubmit () {
      // 获取输入框的值/校验/交给后端/验证是否成功
      // console.log(this.user)
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true,
        duration: 0
      })
      try {
        // 如果login调用报错就进入catch
        // 用于后端直接返回错误情况,如后端返回状态码可以按正常方式写
        const { data } = await login(this.user)
        // console.log(data)
        this.setUser(data)
        this.$toast.success('登录成功')
        this.$router.push('/my')
      } catch (err) {
        // console.dir(err)
        if (err.response.status === 400) return this.$toast.fail('参数无效')
        this.$toast.fail('服务器错误')
      }
    },
    async onSendSms () {
      // 校验手机号 倒计时 请求发送验证码
      // console.log(this.$refs.loginForm)
      try {
        // 验证手机号是否格式正确
        await this.$refs.loginForm.validate('mobile')
        this.isCountDownShow = true
      } catch (err) {
        // console.log(err)
        return false
      }
      // console.log('验证成功')
      try {
        const res = await getCode(this.user.mobile)
        // console.log(res)
        if (res.request.status === 200) return this.$toast.success('发送成功')
      } catch (err) {
        this.isCountDownShow = false
        // console.dir(err)
        if (err.response.status === 429) return this.$toast.fail('请勿频繁发送')
        this.$toast.fail(err.response.statusText)
      }
    },
    onClickLeft () {
      this.$router.back()
    }
  }
}
</script>

<style scoped lang='less'>
.login-box {
  .iconfont {
  font-size: 37px;
  }
  .send-code-btn {
    width: 152px;
    height: 46px;
    border-radius: 23px;
    background-color: #ededed;
    font-size: 22px;
  }
  .login-btn-box {
    margin: 53px 28px;
    .van-button--info{
      background-color: #6db4fb;
    }
  }
}

</style>
